package com.mzy.linear_struct.面试150题;


/**
 * @Author: mizy3
 * @Date: 2024/08/15/下午2:56
 * @Description:
 */
public class Pro100相同的树 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        return preorderTraverse(p, q);
    }

    private boolean preorderTraverse(TreeNode p, TreeNode q) {
        if(p == null && q== null){
            return true;
        }
        if (p == null || q == null) {
            return false;
        }

        if (p.val != q.val) {
            return false;
        }


        return preorderTraverse(p.left, q.left) == preorderTraverse(p.right, q.right);

    }

    public static void main(String[] args) {
        TreeNode p = new TreeNode(10, new TreeNode(5), new TreeNode(15));
        TreeNode q = new TreeNode(10, new TreeNode(5), null);
        System.out.println(new Pro100相同的树().preorderTraverse(p, q));
    }
}
